using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace XeoEngine
{
    class LoadingScreen : GameScreen
    {
        bool loadingIsSlow;
        bool otherScreensAreGone;
        GameScreen[] screensToLoad;
        Texture2D backgroundTexture;
        ContentManager content;
        string tiptext;

        private LoadingScreen(ScreenManager screenManager, bool loadingIsSlow, string tip, GameScreen[] screensToLoad)
        {
            this.loadingIsSlow = loadingIsSlow;
            this.screensToLoad = screensToLoad;
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            tiptext = tip;
        }

        public static void Load(ScreenManager screenManager, bool loadingIsSlow, string tip, PlayerIndex? controllingPlayer, params GameScreen[] screensToLoad)
        {
            foreach (GameScreen screen in screenManager.GetScreens()) screen.ExitScreen();
            LoadingScreen loadingScreen = new LoadingScreen(screenManager, loadingIsSlow, tip, screensToLoad);
            screenManager.AddScreen(loadingScreen, controllingPlayer);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (otherScreensAreGone)
            {
                ScreenManager.RemoveScreen(this);
                foreach (GameScreen screen in screensToLoad)
                {
                    if (screen != null)
                    {
                        ScreenManager.AddScreen(screen, ControllingPlayer);
                    }
                }
                ScreenManager.Game.ResetElapsedTime();
            }
        }

        public override void LoadContent()
        {
            if (content == null) content = new ContentManager(ScreenManager.Game.Services, "Content");
            backgroundTexture = content.Load<Texture2D>("Menu/background");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if ((ScreenState == ScreenState.Active) && (ScreenManager.GetScreens().Length == 1))
            {
                otherScreensAreGone = true;
            }
            if (loadingIsSlow)
            {
                SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
                SpriteFont font = ScreenManager.Font;
                string message = Localizations.Strings.Loading;
                Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
                Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
                Vector2 textSize = font.MeasureString(message);
                Vector2 textPosition = (viewportSize - textSize) / 2;
                textPosition.Y /= 2;
                Color color = Color.White * TransitionAlpha;
                Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
                spriteBatch.Begin();
                spriteBatch.Draw(backgroundTexture, fullscreen, new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));
                spriteBatch.DrawString(font, message, textPosition, color);
                textPosition=(viewportSize - font.MeasureString(tiptext)) / 2;
                textPosition.Y *= 2;
                spriteBatch.DrawString(font, tiptext, textPosition, Color.DeepSkyBlue * TransitionAlpha);
                spriteBatch.End();
            }
        }
    }
}